/**
 * 
 */
package com.acme.bankapp.domain.bank;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Labun Konstantin
 * 
 */
public class Bank {

    public class DebugListener implements ClientRegistrationListener {
	public DebugListener() {
	}

	@Override
	public void onClientAdded(Client client) {
	    System.out.println(Calendar.getInstance().getTime().toString()
		    + ". " + client);
	}
    }

    public class EmailNotificationListener implements
	    ClientRegistrationListener {
	public EmailNotificationListener() {
	}

	@Override
	public void onClientAdded(Client client) {
	    System.out.println("Notification email for client " + client
		    + " to be sent.");
	}
    }

    public class PrintClientListener implements ClientRegistrationListener {
	public PrintClientListener() {
	}

	@Override
	public void onClientAdded(Client client) {
	    System.out.println(client);
	}
    }

    private static final int CLIENTS_MAX = 10;

    private Client[] clients = new Client[CLIENTS_MAX];

    private List<ClientRegistrationListener> listeners = new LinkedList<>();

    public Bank() {
	PrintClientListener printer = new PrintClientListener();
	EmailNotificationListener mailNotif = new EmailNotificationListener();
	DebugListener debugger = new DebugListener();
	listeners.add(printer);
	listeners.add(mailNotif);
	listeners.add(debugger);
    }

    public Bank(ClientRegistrationListener[] listeners) {
	for (ClientRegistrationListener listener : listeners) {
	    this.listeners.add(listener);
	}
    }

    public void addClient(Client newClient) {
	for (int i = 0; i < CLIENTS_MAX; i++) {
	    if (clients[i] == null) {
		clients[i] = newClient;
		break;
	    }
	}
	for (ClientRegistrationListener listener : listeners) {
	    listener.onClientAdded(newClient);
	}
    }

    /**
     * @return the clients
     */
    public Client getClient(String clientName) {
	Client result = null;
	for (int i = 0; i < CLIENTS_MAX; i++) {
	    if (clients[i] != null && clients[i].getName().equals(clientName)) {
		result = clients[i];
	    }
	}
	return result;
    }

    /**
     * @return the clients
     */
    public Client[] getClients() {
	return clients;
    }
}
